<?php
namespace app\admin\controller;
use think\Controller;

class Basewxget extends Controller{

    public function test(){
        $access_token=$this->gettoken();
        $url="https://api.weixin.qq.com/cgi-bin/user/get?access_token=".$access_token."&next_openid=";
        $result=file_get_contents($url);
        echo "<pre>";
        var_dump($result);
    }





	public function gettoken(){
        $redis=new \Redis;
        $redis->connect("127.0.0.1",6379);
        $redis->auth("zaozao2019@");
        if($redis->exists("wx_access_token")){
            return $redis->get("wx_access_token");
        }else{
            $appid="wx352362f198d9c56b";
            $secret="891f9c8031c893848960d665e218f9ce";
            $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid."&secret=".$secret;
            $result=file_get_contents($url);

            $result=json_decode($result,true);

            $redis->set("wx_access_token", $result["access_token"], 7200);
            return $result["access_token"];
        }

        //echo "<pre>";
        //var_dump($result);
    }
	public function getuserinfo(){
		$code = request()->get('code');
		$appid="wx352362f198d9c56b";
		$secret="891f9c8031c893848960d665e218f9ce";     
		//用通过code换取网页授权access_token，同时获得openid
        $url = 'https://api.weixin.qq.com/sns/oauth2/access_token?appid='.$appid.'&secret='.$secret.'&code='.$code.'&grant_type=authorization_code';
		$userinfo=$this->getJson($url);


		//echo session('url');die;
		if($userinfo){
            session("user_id",$userinfo["openid"]);
            /*$access_token=$this->gettoken();
            $userurl="https://api.weixin.qq.com/cgi-bin/user/info?access_token=".$access_token."&openid=".$userinfo["openid"]."&lang=zh_CN";

            $result=file_get_contents($userurl);
            $res=json_decode($result,true);
            $wxuser=Db::table("zz_wxuser")->where("openid",$userinfo["openid"])->find();

            $user["headimgurl"]=$res["headimgurl"];
            $user["subscribe"]=$res["subscribe"];
            $user["nickname"]=$res["nickname"];
            $user["city"]=$res["city"];
            $user["province"]=$res["province"];
            $user["country"]=$res["country"];
            if(empty($wxuser)){
                $user["openid"]=$userinfo["openid"];
                Db::table("zz_wxuser")->insert($user);
            }else{
                $user["update_time"]=date("Y-m-d H:i:s");
                Db::table("zz_wxuser")->where("openid",$userinfo["openid"])->update($user);
            }*/

			header("Location:".session('url'));
			exit;
		}else{
			return "获取用户信息失败";
		}
	}
	function getJson($url){
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); 
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        $output = curl_exec($ch);
        curl_close($ch);
        return json_decode($output, true);
    } 
	
}